<template>
  <a-modal
    title="调账"
    :width="500"
    :visible="visible"
    @ok="handleModalConfirm"
    @cancel="handleModalCancel"
    :confirm-loading="modalLoading"
  >
    <a-form-model
      ref="form"
      :model="form"
      :label-col="{ span: 4 }"
      :wrapper-col="{ span: 20 }"
    >
    
      <a-form-model-item label="结算数量">
        <a-input-number
          v-model="form.clearCount"
          :min="0"
          placeholder="请输入"
          style="width: 200px"
        />
      </a-form-model-item>
      <a-form-model-item label="消耗">
        <a-input-number
          v-model="form.useAmount"
          :min="0"
          placeholder="请输入"
          style="width: 200px"
        />
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
import * as Ajax from "@/api/bill";
export default {
  props: {
    visible: {
      type: Boolean,
    },
    id: {
      type: String,
      default: "",
    },
    loanId: {
      type: String,
      default: "",
    },
    time: {
      type: String,
      default: "",
    }
  },
  data() {
    return {
      modalLoading: false,
      form: {
        useAmount: "",
        clearCount: "",
      },

      typeOptions: [
        {
          label: "增加",
          value: 0,
        },
        {
          label: "减少",
          value: 1,
        },
      ],
    };
  },
  watch: {
    visible(val) {
      if (val) {
        if (this.isEdit) {
          this.form = JSON.parse(JSON.stringify(this.default));
        }
      } else {
      }
    },
  },
  methods: {
    handleModalConfirm() {
  this.modalLoading = true;
  const params = { ...this.form, loanId: this.loanId, time: this.time ,id:this.id};
  console.log(params);

  // 检查是否有值，排除空字符串
  const hasUseAmount = params.useAmount !== "" && params.useAmount !== undefined && params.useAmount !== null;
  const hasClearCount = params.clearCount !== "" && params.clearCount !== undefined && params.clearCount !== null;

  if (hasUseAmount && hasClearCount) {
    Ajax.fixeduse2total(params)
      .then(({ success }) => {
        if (success) {
          this.$message.success("操作成功");
          this.$emit("success");
          this.handleModalCancel();
        }
      })
      .finally(() => {
        this.modalLoading = false;
      });
  } else {
    if (hasUseAmount) {
      Ajax.shareuse2(params)
        .then(({ success }) => {
          if (success) {
            this.$message.success("操作成功");
            this.$emit("success");
            this.handleModalCancel();
          }
        })
        .finally(() => {
          this.modalLoading = false;
        });
    } else {
      Ajax.fixeduse2(params)
        .then(({ success }) => {
          if (success) {
            this.$message.success("操作成功");
            this.$emit("success");
            this.handleModalCancel();
          }
        })
        .finally(() => {
          this.modalLoading = false;
        });
    }
  }
},

    handleModalCancel() {
      this.form = this.$options.data().form;
      this.$refs.form.resetFields();
      this.$emit("close");
    },
  },
};
</script>

<style lang="scss" scoped>
.table-wrap {
  margin-top: 20px;
  padding-top: 16px;
}

.btn-gruop {
  display: flex;
  justify-content: flex-end;
}

::v-deep .ant-form-item {
  &:last-child {
    margin-bottom: 0;
  }
}
</style>
